package com.nowcoder.community.config;

import com.nowcoder.community.controller.interceptor.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @BelongsProject: community
 * @BelongsPackage: com.nowcoder.community.config
 * @Author: Master_XXJS
 * @CreateTime: 2022-09-26  14:36
 * @Description: TODO
 * @Version: 1.0
 */
//实现 mvc 提供的拦截器接口，进行魔改
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /*
        注入自定义的拦截器
     */
    @Autowired
    private AlphaInterceptor alphaInterceptor;
    @Autowired
    private LoginTicketInterceptor loginTicketInterceptor;
    //不需要 进行 登录的拦截了，我们换了 security 进行过滤
    //@Autowired
    //private LoginRequiredInterceptor loginRequiredInterceptor;
    @Autowired
    private MessageInterceotor messageInterceotor;
    @Autowired
    private DateInterceptor dateInterceptor;

    /**
     * 添加我们自定义的拦截器
     * @param registry 进行注册拦截器
     *        excludePathPatterns(里面是排除的资源，不进行拦截，一般是静态资源的路径，比如css，js那些)
     *        addPathPatterns（里面是进行拦截的路径，如果没有配置这个选项，默认全部拦截）
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /*
           下面是注册了两个拦截器，多拦截器，是按照 注册的顺序进行拦截的
         */
        registry.addInterceptor(loginTicketInterceptor)
                .excludePathPatterns("/**/*.css","/**/*.png","/**/*.jpg","/**/*.js","/**/*.jpeg","/**/kaptcha/**");
                //.addPathPatterns("/login");
                //.addPathPatterns("/login/**");
                //根路径 localhost：8080/community，访问 localhost:8080/community/login，因为login前面没有路径了，所以不需要/**


        //registry.addInterceptor(loginRequiredInterceptor)
        //        .excludePathPatterns("/**/*.css","/**/*.png","/**/*.jpg","/**/*.js","/**/*.jpeg","/**/kaptcha/**");

        //消息 Message 的未读数量，经常进行刷新
        registry.addInterceptor(messageInterceotor)
                 .excludePathPatterns("/**/*.css","/**/*.png","/**/*.jpg","/**/*.js","/**/*.jpeg","/**/kaptcha/**");

        //拦截 controller 的访问资源，记录 UV，DAV 网站的统计数量
        registry.addInterceptor(dateInterceptor)
                 .excludePathPatterns("/**/*.css","/**/*.png","/**/*.jpg","/**/*.js","/**/*.jpeg","/**/kaptcha/**");
    }
}

